﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="FBcallBack.aspx.cs" Inherits="haber.FBcallBack" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <script src="Scripts/jquery-1.7.1.js" type="text/javascript"></script>
    <script src="Scripts/jquery-ui-1.8.16.js" type="text/javascript"></script>
    <script src="Scripts/jquery.json-2.2.js" type="text/javascript"></script>
    <title></title>
     <script  type="text/javascript">
         var token = "<%=tokens%>";
         var id = "<%=id%>";
         var pagename = "";
         var pageID = "";
         if (token != null) {
             if (id == null) {
                 alert("id null");               
             }
             $.ajax({
                 type: "get",
                 url: "https://graph.facebook.com/me?access_token=" + token,
                 data: "",
                 dataType: "json",
                 success: function (data) {
                  //   alert('');
                     //                     $('#Adi').html(data.first_name);
                     //                     $('#Soyadi').html(data.last_name);
                     $('#KullaniciAdi').html(data.username);
                     if (data.username) {

                         $.ajax({
                             type: "POST",
                             contentType: "application/json; charset=utf-8",
                             url: 'https://api.facebook.com/method/fql.query?query=' + "SELECT page_id, type from page_admin WHERE uid=" + data.id + '&access_token=' + token + '&format=json',
                             dataType: "jsonp",
                             success: function (resp) {
                               //  alert('2');
                                 if (resp.length > 0) {

                                     pageID = resp[0].page_id;

                                     $.ajax({
                                         type: "POST",
                                         contentType: "application/json; charset=utf-8",
                                         url: 'https://api.facebook.com/method/fql.query?query=' + "SELECT name from page WHERE page_id=" + resp[0].page_id + '&access_token=' + token + '&format=json',
                                         dataType: "jsonp",
                                         success: function (response) {
                                             var pageAccToken = "";

                                             if (response.length > 0) {
                                                 $.ajax({
                                                     type: "GET",
                                                     url: "https://graph.facebook.com/" + data.id + "/accounts?access_token=" + token,
                                                     dataType: "json",
                                                     data: "",
                                                     success: function (d) {
                                                         pageAccToken = d.data[0].access_token;
                                                         // alert(d.data[0].access_token);
                                                         pagename = response[0].name;
                                                         var jsons = { id: id, fbAd: data.username, accesstoken: token, fbID: data.id, pageID: pageID, pageName: pagename, pageAccToken: pageAccToken };
                                                         $.ajax({
                                                             contentType: "application/json; charset=utf-8",
                                                             type: "POST",
                                                             url: "FBFuncs.aspx/FbKaydet",
                                                             data: $.toJSON(jsons),
                                                             dataType: "json",
                                                             success: function (data) {


                                                                 window.location = "/Yonetim/Default.aspx?katid=" + id;

                                                             },
                                                             error: function (xhr, status, Error) {
                                                                 alert(xhr.Message);
                                                             }
                                                         });
                                                     },
                                                     error: function () {
                                                         ekranAc();

                                                     }
                                                 });
                                               

                                             } else {
                                                 jsons = { id: id, fbAd: data.username, accesstoken: token, fbID: data.id, pageID: pageID, pageName: pagename };
                                                 $.ajax({
                                                     contentType: "application/json; charset=utf-8",
                                                     type: "POST",
                                                     url: "FBFuncs.aspx/FbKaydet",
                                                     data: $.toJSON(jsons),
                                                     dataType: "json",
                                                     success: function (data) {


                                                         window.location = "/Yonetim/Default.aspx?katid=" + id;

                                                     },
                                                     error: function (xhr, status, Error) {
                                                         alert(xhr.Message);
                                                     }
                                                 });


                                             }
                                         },
                                         error: function () {
                                             ekranAc();

                                         }
                                     });



                                 }




                             },
                             error: function () {
                                 ekranAc();

                             }
                         });






                     }
                 },
                 error: function (xhr, status, Error) {
                     alert(xhr.Message);
                 }
             });


         }

            
        
    
    
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        Yönlendiriliyor...</div>
    </form>
</body>
</html>
